package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.service.CategoryService;
import net.zjitc.utils.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Value("${local.server.url}")
    private String localServerUrl;

    @Value("${file.upload-dir}")
    private String uploadDir;

    @Value("${fileCategory.upload-dir}")
    private String uploadDirCategory;

    @GetMapping("/findAll")
    @ResponseBody
    public APIResponse<List<Category>> findAll(){
        APIResponse<List<Category>> apiResponse = new APIResponse<>();
        List<Category> categoryList = categoryService.findAll();
        if(categoryList!=null){
            apiResponse.success("查询成功", categoryList);
            return apiResponse;
        }
        apiResponse.fail(1000,"查询失败", null);
        return apiResponse;
    }
    @GetMapping("/findOne")
    @ResponseBody
    public APIResponse<Category> findOne(@RequestBody @RequestParam("id")int id){
        APIResponse<Category> apiResponse = new APIResponse<>();
        try{
            Category category = categoryService.findOne(id);
            apiResponse.success("查询成功", category);
            return apiResponse;
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.fail(1000,"查询失败",null);
            return apiResponse;
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public APIResponse<Category> update(@RequestBody Category category){
        APIResponse<Category> apiResponse = new APIResponse<>();
        try{
            String picture = category.getPicture();
            if(picture!=null&&picture.contains(localServerUrl)){
                category.setPicture(picture.replace(localServerUrl,""));
            }
            if(picture!=null&&!picture.contains("static/")){
                category.setPicture(uploadDir+picture);
            }
            System.out.println(category);
            categoryService.update(category);
            apiResponse.success("修改成功",category);
            return apiResponse;
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.fail(1000,"修改失败",null);
            return apiResponse;
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    public APIResponse<Boolean> delete(@RequestBody @RequestParam("id")int id){
        APIResponse<Boolean> apiResponse = new APIResponse<>();
        try{
            categoryService.delete(id);
            System.out.println(id);
            apiResponse.success("删除成功",true);
            return apiResponse;
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.fail(1000,"删除失败",false);
            return apiResponse;
        }
    }

    @PostMapping("/add")
    @ResponseBody
    public APIResponse<Category> add(@RequestBody Category category){
        APIResponse<Category> apiResponse = new APIResponse<>();
        try{
            String picture = category.getPicture();
            if(picture!=null&&picture.contains(localServerUrl)){
                category.setPicture(picture.replace(localServerUrl,""));
            }
            if(picture!=null&&!picture.contains("static/")){
                category.setPicture(uploadDirCategory+picture);
            }
            System.out.println(category);
            if(category.getPid()==null){
                category.setPid(0);
            }
            categoryService.add(category);
            apiResponse.success("添加成功",category);
            return apiResponse;
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.fail(1000,"添加失败",null);
            return apiResponse;
        }
    }
}